const { tree } = require('./tree')

/**
 * 返回当前及其所有父级节点
 * @param {*} tree
 * @param {*} value
 * @param {*} path
 * @param {*} key
 * @param {*} children
 * @returns
 */

const filterPidByQueue = (tree, value, path = [], key = 'id', children = 'children') => {
  if (!tree) return []
  for (const data of tree) {
    path.push(data[key]) // 缓存父级
    if (data[key] === value) return path
    if (data[children]?.length) {
      const list = filterPidByQueue(data[children], value, path)
      if (list?.length) return list
    }
    path.pop() // 没有查找到直接删除
  }
  return path
}

const arr = filterPidByQueue(tree, 5)
console.log(arr)
